package com.wbs.shop.nuby.view.dialog;

import android.app.Dialog;
import android.content.Context;
import androidx.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.wbs.shop.nuby.R;
import com.wbs.shop.nuby.bean.GoodsBean;
import com.wbs.shop.nuby.bean.GoodsShowBean;
import com.wbs.shop.nuby.bean.SpecBean;
import com.wbs.shop.nuby.helper.CashierHelper;
import com.wbs.shop.nuby.utils.GlideUtils;
import com.wbs.shop.nuby.utils.Logger;
import com.wbs.shop.nuby.utils.Utils;
import com.wbs.shop.nuby.widget.AddWidget;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by zhoucl on 2017/12/22 0022.
 */

public class GoodsSpecDialog extends Dialog {
    @BindView(R.id.ivIcon)
    ImageView ivIcon;
    @BindView(R.id.tvMoney)
    TextView tvMoney;
    @BindView(R.id.tvName)
    TextView tvName;
    @BindView(R.id.addwidget)
    AddWidget addwidget;
    @BindView(R.id.flowlayout)
    TagFlowLayout mFlowLayout;
    private List<SpecBean> specBeans;
    private GoodsBean goodsBean;
    private int mPosition = 0;
    private OnSelectGoodsSpecListener mOnSelectGoodsSpecListener;

    private GoodsSpecDialog(@NonNull Context context, List<SpecBean> specBeans, GoodsBean goodsBean, OnSelectGoodsSpecListener mOnSelectGoodsSpecListener) {
        super(context, R.style.loadingdialog);
        this.specBeans = specBeans;
        this.goodsBean = goodsBean;
        this.mOnSelectGoodsSpecListener = mOnSelectGoodsSpecListener;
        init();
    }

    private void init() {
        setContentView(R.layout.dialog_goods_spec);
        ButterKnife.bind(this);
        GlideUtils.load(getContext(), goodsBean.getImgUrl(), ivIcon);
        tvName.setText(goodsBean.getName());
        TagAdapter<SpecBean> tagAdapter = new TagAdapter<SpecBean>(specBeans) {
            @Override
            public View getView(FlowLayout parent, int position, SpecBean specBean) {
                TextView tv = (TextView) getLayoutInflater().inflate(R.layout.item_select_spec, mFlowLayout, false);
                tv.setText(specBean.getSpecName());
                return tv;
            }
        };
        mFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                mPosition = position;
                changMoney();
                return false;
            }
        });
        tagAdapter.setSelectedList(0);
        mFlowLayout.setAdapter(tagAdapter);
        changMoney();
        addwidget.setOnNumberChangListener((v, number) -> changMoney());
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        attributes.width = getContext().getResources().getDisplayMetrics().widthPixels;
        getWindow().setAttributes(attributes);
        getWindow().setGravity(Gravity.BOTTOM);
    }

    private void changMoney() {
        try {
            SpecBean specBean = specBeans.get(mPosition);
            tvMoney.setText(getContext().getString(R.string.text_money, new BigDecimal(specBean.getPrice()).multiply(new BigDecimal(addwidget.getNumber())).setScale(2, RoundingMode.HALF_UP).toEngineeringString()));
        } catch (Exception e) {
            Logger.e(e);
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
        specBeans = null;
        goodsBean = null;
    }

    @OnClick({R.id.confirm})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.confirm:
                GoodsShowBean goodsShowBean = CashierHelper.goodsTOGoodsShow(goodsBean, specBeans.get(mPosition), addwidget.getNumber());
                if (mOnSelectGoodsSpecListener != null)
                    mOnSelectGoodsSpecListener.onSelect(goodsShowBean);
                dismiss();
                break;
        }
    }

    public interface OnSelectGoodsSpecListener {
        void onSelect(GoodsShowBean goodsShowBean);
    }

    public static class Builder {
        private Context context;
        private List<SpecBean> specBeans;
        private GoodsBean goodsBean;
        private OnSelectGoodsSpecListener mOnSelectGoodsSpecListener;

        public Builder(@NonNull Context context) {
            this.context = context;
        }

        public GoodsSpecDialog create() {
            return new GoodsSpecDialog(context, specBeans, goodsBean, mOnSelectGoodsSpecListener);
        }

        public Builder onSelectGoodsSpecListener(OnSelectGoodsSpecListener mOnSelectGoodsSpecListener) {
            this.mOnSelectGoodsSpecListener = mOnSelectGoodsSpecListener;
            return this;
        }

        public Builder goodsBean(GoodsBean goodsBean) {
            this.goodsBean = goodsBean;
            return this;
        }

        public Builder specBeanList(List<SpecBean> specBeans) {
            this.specBeans = specBeans;
            return this;
        }
    }
}
